package com.cooper.scraper;

import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.res.Resources.NotFoundException;

public class Scraper {

	public static boolean parseBool(String value, boolean defaultValue) {
		if (value == null || "".equals(value.trim())) {
			return defaultValue;
		} else
			return value.trim().equals("yes");
	}

	public Scraper(String id, String scraperText, Map<String, String> params,
			DownloadParams downloadParams) {
		this.id = id;
		this.scraperText = scraperText;
		this.params = params;
		this.downloadParams = downloadParams;
		clearBuffer();
	}

	public void clearBuffer() {
		for (int i = 0; i < buffer.length; i++) {
			buffer[i] = "";
		}
	}

	private String id;
	protected String scraperText;
	protected Map<String, String> params;
	protected DownloadParams downloadParams;

	private String[] buffer = new String[21];

	public Scraper clonScraper() {
		return new Scraper(id, scraperText, params, downloadParams);
	}
	
	public String getId(){
		return id;
	}

	protected void setBufferValue(int index, String value) {
		buffer[index] = (value == null ? "" : value);
	}

	String[] getBuffer() {
		return buffer;
	}

	private HttpDownload httpDownload = null;

	HttpDownload getHttpDownload() {
		if (httpDownload == null) {
			httpDownload = new HttpDownload(downloadParams);
		}
		return httpDownload;
	}

	public Map<String, String> getParams() {
		return params;
	}

	protected DefineTag getDefineTag(String tagName) throws NotFoundException,
			XmlPullParserException, IOException {

		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		factory.setNamespaceAware(true);
		XmlPullParser xpp = factory.newPullParser();
		xpp.setInput(new StringReader(scraperText));

		return DefineTag.defineTagFactory(this, xpp, tagName);
	}

	protected Map<String, String> getResultParam(String xml, String keyTagName,
			String keyAttrName) throws XmlPullParserException, IOException {
		Map<String, String> result = new HashMap<String, String>();
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		factory.setNamespaceAware(true);
		XmlPullParser xpp = factory.newPullParser();
		xpp.setInput(new StringReader(xml));
		int eventType = xpp.getEventType();
		boolean inSortTag = false;
		String key = "";
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG) {
				if (keyTagName.equals(xpp.getName())) {
					inSortTag = true;
					key = xpp.getAttributeValue(null, keyAttrName);
				}
			} else if (eventType == XmlPullParser.TEXT) {
				if (inSortTag) {
					result.put(key, xpp.getText());
				}
			} else if (eventType == XmlPullParser.END_TAG) {
				if (keyTagName.equals(xpp.getName())) {
					inSortTag = false;
				}
			}
			eventType = xpp.next();
		}
		return result;
	}
	
	@Override
	public int hashCode(){
		return id.hashCode();
	}

	@Override
	public boolean equals(Object object) {
		if (object == null) {
			return false;
		}
		if (this == object) {
			return true;
		}
		if (object instanceof Scraper) {
			return ((Scraper) object).id.equals(id);
		} else
			return false;
	}

}
